<template>
  <div class="TODO-footer">
    <label>
      <input type="checkbox" v-model="ischeckAll" />
    </label>
    <span
      ><span>已完成{{ count }}</span
      >/全部{{ todos.length }}</span
    >
    <button @click="delTodo">清除已完成的任务</button>
  </div>
</template>
<script lang="ts">
import { computed, defineComponent } from 'vue'
import { Todo } from '../index'
export default defineComponent({
  name: 'Footer',
  props: {
    todos: {
      type: Array as () => Todo[],
      required: true
    },
    checkAll: {
      type: Function,
      required: true
    },
    filterTodos: {
      type: Function,
      required: true
    }
  },
  setup (props) {
    const count = computed(() => {
      return props.todos.reduce(
        (pre, todo, index) => pre + (todo.iscompleted ? 1 : 0),
        0
      )
    })
    const ischeckAll = computed({
      get () {
        return count.value > 0 && props.todos.length === count.value
      },
      set (val) {
        props.checkAll(val)
      }
    })
    const delTodo = () => {
      props.filterTodos()
    }
    return {
      count,
      ischeckAll,
      delTodo
    }
  }
})
</script>
<style scoped lang="less">
.TODO-footer {
  width: 100%;
  height: 40px;
  display: flex;
  align-items: center;
  margin-top: 20px;
  label {
    margin-right: 10px;
  }
  span {
    font-weight: bold;
  }
  button {
    margin-left: 90px;
    border: none;
    background: rgb(226, 113, 113);
  }
}
</style>
